import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            // 获取当前线程的引用
            Thread cur = Thread.currentThread();
            while (!cur.isInterrupted()){
                System.out.println("hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            System.out.println("t线程退出");
        });

        t.start();

        System.out.println("请输入一个整数：0表示让 t 线程终止");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n == 0){
            // 这个方法不仅可以设置标志位，还能唤醒 sleep 等导致线程阻塞的方法
            // 会使 sleep 抛出InterruptedException异常
            t.interrupt();
        }
    }
}
